//
//  IWFriendsViewController.m
//  ItcastWeibo
//
//  Created by mj on 14-1-15.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

#import "IWFriendsViewController.h"
#import "AyUserTool.h"
#import "IWFriendshipResult.h"
#import "IWFriendshipParam.h"
#import "AyAccountTool.h"

@interface IWFriendsViewController ()

@end

@implementation IWFriendsViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"全部关注";
}

- (void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView
{
    if (refreshView == _header) {
        [self loadNewFriends];
    } else {
        [self loadMoreFriends];
    }
}

- (void)loadNewFriends
{
    _param.cursor = @(_result.previous_cursor);
       _param.uid = @([AyAccountTool account].uid);
    [AyUserTool friendsWithParam:_param success:^(IWFriendshipResult *result) {
        _result = result;
        // 1.新数据
        NSMutableArray *newUsers = [NSMutableArray arrayWithArray:result.users];
        [newUsers addObjectsFromArray:_friendships];
        _friendships = newUsers;
        
        // 3.刷新表格
        [self.tableView reloadData];
        
        // 4.刷新控件
        [_header endRefreshing];
    } failure:^(NSError *error){
        [_header endRefreshing];
    }];
}

- (void)loadMoreFriends
{
    _param.cursor = @(_result.next_cursor);
       _param.uid = @([AyAccountTool account].uid);
    [AyUserTool friendsWithParam:_param success:^(IWFriendshipResult *result) {
        _result = result;
        // 1.新数据
        [_friendships addObjectsFromArray:result.users];
        
        // 2.刷新表格
        [self.tableView reloadData];
        
        // 3.刷新控件
        [_footer endRefreshing];
    } failure:^(NSError *error){
        [_footer endRefreshing];
    }];
}

@end
